<template>
  <div class="c-flex-column c-aligni-center c-ww52">
    <div @click="cancleCollect" v-if="hasCollect" class="iconfont c-fs36 icon-yishoucang theme-fc"></div>
    <div @click="doCollect" v-else class="iconfont c-fs36 icon-shoucang1 c-fc-xblack"></div>
    <div class="c-fs16 c-fc-xblack c-mt4">收藏<!-- {{hasCollect ? '已收藏' : '收藏'}} --></div>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js";
import { getBindPhoneSetting } from "@/utils/checkUnBindPhone.js";
let isClick = false;
export default {
  name: "CollectBtnCom",
  components: {
  },
  props: {
    hasCollect: {
      type: Boolean,
      default: false
    },
    prodType: {
      type: Number,
      default: 0,
    },
    prodId: {
      type: Number,
      default: 0,
    },
    subProdType: {
      type: Number,
      default: 0,
    },
    needBindPhone: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      pordTypeName: {
        '5': '课程',
        '9': '专栏',
        '61': '知识套餐',
        '8': '资料'
      },
      bindPhoneSetting: null
    };
  },
  mounted() {
    if (localStorage.getItem("isVisitor")) {
      getBindPhoneSetting().then(res => { // 游客模式绑定手机号
        this.bindPhoneSetting = res;
      });
    }
  },
  watch: {
  },
  methods: {
    cancleCollect() {
      if (isClick) {
        return;
      }
      isClick = true;
      if (this.bindPhoneSetting && this.bindPhoneSetting.isVisitorBind) {
        if (utilJs.goBindMobile()) return;
      }
      let postData = {
        prodType: this.prodType,
        prodId: this.prodId,
      }
      utilJs.postMethod(`${global.apiurl}prodCollect/cancelCollect`, postData, (res) => {
        this.$showCjToast({
          text: "收藏已取消",
          type: 'text',
        });
        isClick = false;
        this.$emit('cancelCollect')
      });
    },
    doCollect() {
      if (isClick) {
        return;
      }
      isClick = true;
      if ((this.bindPhoneSetting && this.bindPhoneSetting.isVisitorBind) || this.needBindPhone) {
        if (utilJs.goBindMobile()) return;
      }
      let postData = {
        prodType: this.prodType,
        prodId: this.prodId,
      }
      if (this.prodType == global.ckFrom.course) {
        postData.subProdType = this.subProdType;
      }
      utilJs.postMethod(`${global.apiurl}prodCollect/addToCollect`, postData, (res) => {
        this.$showCjToast({
          text: this.pordTypeName[this.prodType] + "已收藏",
          type: 'text',
        });
        isClick = false;
        this.$emit('doCollect')
      });
    }
  }
};
 </script>


<style scoped>
</style>
